﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KeMei.Common;

public partial class Manager_Users_Edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserInfo"] == null)
        {
            Response.Redirect(ConfigHelper.SystemUrl + "Manager/login.aspx", true);

        }
        if (!IsPostBack)
        {
            this.InitUserRole();
            if (Request["ID"] != null)
            {
                this.InitModel(int.Parse(Request["ID"].ToString()));
            }
        }
    }

    /// <summary>
    /// 绑定实体类到控件
    /// </summary>
    protected void InitModel(int ID)
    {
        KeMei.Model.SysUser model = null;
        KeMei.BLL.SysUser bll = new KeMei.BLL.SysUser();

        try
        {
            model = bll.GetModel(ID);

            if (model != null)
            {
                txtName.Text = model.TName;
                txtReallyName.Text = model.ReallyName;
                txtPassword.Text = DESEncrypt.Decrypt(model.Password);
                ddlUserRole.SelectedValue = model.RoleID.ToString();
            }
        }
        catch (Exception ex)
        {

            ExtAspNet.Alert.Show("初始化实体类失败：" + ex.Message);
        }
    }


    /// <summary>
    /// 初始化栏目
    /// </summary>
    protected void InitUserRole()
    {
        try
        {
            KeMei.BLL.SysUserRole bll = new KeMei.BLL.SysUserRole();

            ddlUserRole.DataSource = bll.GetModelList("");
            ddlUserRole.DataTextField = "TName";
            ddlUserRole.DataValueField = "ID";
            ddlUserRole.DataBind();
        }
        catch (Exception ex)
        {

            ExtAspNet.Alert.Show("绑定用户类型：" + ex.Message);
        }
    }




    /// <summary>
    /// 保存
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        KeMei.BLL.SysUser bll = new KeMei.BLL.SysUser();
        KeMei.Model.SysUser model = new KeMei.Model.SysUser();

        try
        {
            model.TName = txtName.Text.Trim();
            model.ReallyName = txtReallyName.Text;
            model.Password =DESEncrypt.Encrypt(txtPassword.Text.Trim());
            model.RoleID = int.Parse(ddlUserRole.SelectedValue);

            if (Request["ID"] != null)
            {
                model.ID = int.Parse(Request["ID"].ToString());

                bll.Update(model);
            }
            else
            {
                bll.Add(model);
            }
            ExtAspNet.Alert.Show("保存成功！", String.Empty, ExtAspNet.Alert.DefaultIcon, ExtAspNet.ActiveWindow.GetHidePostBackReference());
        }
        catch (Exception ex)
        {
            ExtAspNet.Alert.Show("保存失败：" + ex.Message);
        }
    }
}